package com.jt;

import com.jt.mapper.DeptMapper;
import com.jt.mapper.EmpMapper;
import com.jt.mapper.UserAnnoMapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.Dept;
import com.jt.pojo.Emp;
import com.jt.pojo.User;
import org.apache.ibatis.annotations.Select;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@SpringBootTest
class MybatisDemo1ApplicationTests4 {

    @Autowired
    private UserAnnoMapper annoMapper;

    @Test
    //利用注解可以根据返回值类型，自动映射
    public void testFindAll(){
        List<User> list = annoMapper.findAll();
        System.out.println(list);
    }
    @Test
    //利用注解可以根据返回值类型，自动映射
    public void testFindAll2(){
        int id = 1;
        List<User> list = annoMapper.findUserById(id);
        System.out.println(list);
    }
    //根据姓名查询
    @Test
    public void testFindByNa(){
        User user = new User();
        user.setName("王六子");
        List<User> list = annoMapper.findUserByNa(user);
        System.out.println(list);
    }
    //插入数据
    @Test
    public void testInsert(){
        User user = new User();
        user.setName("刘阳").setAge(22).setSex("女");
        int rows = annoMapper.insertUser(user);
        System.out.println(rows);
    }
    //修改数据
    @Test
    public void testSetUser(){
        User user = new User();
        user.setId(236).setName("刘阳").setAge(21).setSex("女");
        int rows = annoMapper.updateUser(user);
        System.out.println(rows);
    }
    //删除数据
    @Test
    public void testDelete(){
        User user = new User();
        user.setId(236);
        int rows = annoMapper.deleteUser(user);
        System.out.println(rows);
    }


    /*测试mybatis的一二级缓存
    * 现象：采用SpringBoot的方式进行测试时，sql执行多次，一级缓存无效
    * 解决：添加事务的注解，SpringBoot中添加了之后，默认采用一个SqlSession
    * */
    @Test
    @Transactional  //添加事务
    public void testCache1(){
        List<User> userList1 = annoMapper.findCache1();
        List<User> userList2 = annoMapper.findCache1();
        List<User> userList3 = annoMapper.findCache1();
        List<User> userList4 = annoMapper.findCache1();
    }


}
